package com.sina.interfaces.web.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sina.interfaces.core.util.Constant;
import com.sina.interfaces.config.DaoConfig;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.*;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;

import static com.sina.interfaces.core.util.JsonUtils.createDefaultObjectMapper;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2017-09-21-18:47
 */
@Configuration
@Import( {DaoConfig.class} )
@ComponentScan({"com.sina.interfaces.dao", "com.sina.interfaces.service"})
public class ServiceConfig implements ResourceLoaderAware {

    private ResourceLoader resourceLoader;

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    @Autowired
    private Environment environment;

    @Bean
    public ResourceBundleMessageSource serviceLayerMessageSource() {
        ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();
        resourceBundleMessageSource.setDefaultEncoding(Constant.UTF8);
        resourceBundleMessageSource.setBasename("i18n/service/messages");
        resourceBundleMessageSource.setUseCodeAsDefaultMessage(true);
        return resourceBundleMessageSource;
    }

    @Bean("commonObjectMapper")
    @Primary
    public ObjectMapper commonObjectMapper(@Qualifier("interfaceSessionFactory") SessionFactory sessionFactory) {
        return createDefaultObjectMapper(sessionFactory);
    }

}
